package qnb.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.Student;
import qnb.entity.Teacher;
import qnb.service.UserService;
import qnb.service.StudentService;
import qnb.service.TeacherServiceDao;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping
public class LoginContorller {
    @Autowired
    private TeacherServiceDao teacherService;
    @Autowired
    private StudentService studentService;

    @Autowired
    private UserService userService;


    private final String LOGIN_USER = "loginUser";

    @RequestMapping("/user/updatePassword")
    @ResponseBody
    public String updatePassword(
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword,
            HttpSession session) {
        return MessageVOFactory.getUpdateMessage(userService.updatePassword(session, oldPassword, newPassword));
    }


    @RequestMapping("/forword/_AdminCenter")
    public ModelAndView forwordAdminCenter(ModelAndView modelAndView, HttpSession session) {
        Object obj = session.getAttribute(LOGIN_USER);
        if (obj != null) {
            if (obj instanceof Student) {
                Student student = (Student) obj;
                modelAndView.addObject("username", student.getStudentName());
            } else if (obj instanceof Teacher) {
                Teacher teacher = (Teacher) obj;
                modelAndView.addObject("username", teacher.getTeacherName());
            }
        }
        modelAndView.setViewName("_AdminCenter");
        return modelAndView;
    }


    /**
     * 跳转
     *
     * @return
     */
    @RequestMapping("/login/forward/login")
    public ModelAndView forwardLogin(ModelAndView modelAndView) {
        modelAndView.setViewName("login/login");
        return modelAndView;
    }

    /**
     * 退出登陆
     *
     * @param session
     * @return
     */
    @RequestMapping("/login/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute(LOGIN_USER);
        return "redirect:/login/forward/login";
    }


    @RequestMapping("/user/getInfo")
    @ResponseBody
    public Map<String, Object> getInfo(HttpSession session) {
        Object obj = session.getAttribute(LOGIN_USER);

        Map<String, Object> map = new HashMap<>();

        if (obj instanceof Student) {
            Student student = (Student) obj;
            student.setStudentPassword(null);
            map.put("type", 1);
        } else if (obj instanceof Teacher) {
            Teacher teacher = (Teacher) obj;
            teacher.setTeacherPassword(null);
            map.put("type", 2);
        }
        map.put("user", obj);

        return map;
    }


    /**
     * 登陆
     *
     * @param account
     * @param password
     * @param remember
     * @return
     */
    @RequestMapping("/login/login")
    @ResponseBody
    public String login(
            @RequestParam("username") String account,
            @RequestParam("password") String password,
            @RequestParam(value = "remember", required = false) Integer remember,
            HttpServletRequest request, HttpServletResponse response) {

        Teacher teacher = teacherService.checkLogin(account, password);
        Student student = studentService.login(account, password);
        HttpSession session = request.getSession();
        if (teacher != null) {
            //存入session
            session.setAttribute(LOGIN_USER, teacher);
        } else if (student != null) {
            //存入session
            session.setAttribute(LOGIN_USER, student);
        } else {
            //账号或密码错误
            return "{\"code\":1}";
        }
        //记住账号
        if (remember != null) {
            Cookie cookie = new Cookie("account", account);
            //24小时
            cookie.setMaxAge(60 * 60 * 24);
            response.addCookie(cookie);
        }
        return "{ \"code\": 0}";
    }
}
